package com.partsoft.dits.integrate;



import java.io.IOException;
import java.util.Properties;

import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.util.StringUtils;

import com.partsoft.dits.ConfigurableBean;
import com.partsoft.dits.DataDescriptorManager;
import com.partsoft.dits.log.Log;

/**
 * LDAP数据发布描述符管理器
 */
public class ContextLdapDescriptorManager extends ConfigurableLdapDataDescriptorManager<LdapEntityEdgeDescriptor>
		implements LdapDataDescriptorManager<LdapEntityEdgeDescriptor>,
		LdapDataDescriptorRegistry<LdapEntityEdgeDescriptor>, DataDescriptorManager<LdapEntityEdgeDescriptor>,
		ConfigurableBean, ApplicationContextAware, ApplicationListener<ApplicationEvent> {

	public ContextLdapDescriptorManager() {
	}

	public ContextLdapDescriptorManager(String contextConfigLocation, String transformScriptDirectoryLocation,
			String descriptorIdPrefix) {
		super(contextConfigLocation, transformScriptDirectoryLocation, descriptorIdPrefix);
	}

	public ContextLdapDescriptorManager(String contextConfigLocation, String transformScriptDirectoryLocation,
			String descriptorIdPrefix, boolean createNoExists) {
		super(contextConfigLocation, transformScriptDirectoryLocation, descriptorIdPrefix, createNoExists);
	}

	@Override
	protected void beforeRemoveDescriptor(LdapEntityEdgeDescriptor descriptor) {
		synchronized (this) {
			if (1 >= countDescriptor(descriptor.getLdapContextId(), descriptor.getEntityParentDN(),
					descriptor.getEntityPropertyId())) {
			}
		}
	}

	@Override
	protected void beforeLoadConfiguredDescriptor(LdapEntityEdgeDescriptor descriptor) {
		try {
			descriptor.setTransformScript(loadTransformScriptContent(descriptor));
		} catch (IOException e) {
			String errMsg = String.format("加载(%s)的转换脚本失败: %s", descriptor.toString(), e.getMessage());
			Log.error(errMsg, e);
			throw new IllegalStateException(errMsg, e);
		}
	}

	@Override
	protected void setupDescriptorIdentifier(LdapEntityEdgeDescriptor descriptor, String id) {
		descriptor.setId(id);
	}

	@Override
	protected Properties beforeAddDescriptor(LdapEntityEdgeDescriptor descriptor) {
		synchronized (this) {
			// 保存脚本
			descriptor.setTransformScriptPath(resolveDescriptorScriptRelativeFileName(descriptor));
			try {
				saveTransformScriptContext(descriptor);
			} catch (IOException e) {
				throw new IllegalStateException(e.getMessage(), e);
			}

			if (0 == countDescriptor(descriptor.getLdapContextId(), descriptor.getEntityParentDN(),
					descriptor.getEntityPropertyId())) {

			}

			Properties beanProps = new Properties();
			beanProps.setProperty("ldapContextId", descriptor.getLdapContextId());
			beanProps.setProperty("entityParentDN", descriptor.getEntityParentDN());
			beanProps.setProperty("entityPropertyId", descriptor.getEntityPropertyId());
			beanProps.setProperty("entityClassName", descriptor.getEntityClassName());
			beanProps.setProperty("entityPropertyNames", descriptor.getEntityPropertyNames());
			beanProps.setProperty("messageCode", descriptor.getMessageCode());
			beanProps.setProperty("transformScriptPath", descriptor.getTransformScriptPath());
			beanProps.setProperty("conversion", new Boolean(descriptor.isConversion()).toString());
			beanProps.setProperty("title", descriptor.getTitle());
			beanProps.setProperty("description", descriptor.getDescription());
			beanProps.setProperty("ordinal", Integer.valueOf(descriptor.getOrdinal()).toString());

			return beanProps;
		}
	}

	@Override
	protected String resolveDescriptorScriptRelativeFileName(LdapEntityEdgeDescriptor descriptor) {
		return StringUtils.hasText(descriptor.getTransformScriptPath()) ? descriptor.getTransformScriptPath() : super
				.resolveDescriptorScriptRelativeFileName(descriptor);
	}

	public Class<LdapEntityEdgeDescriptor> getSupportClass() {
		return LdapEntityEdgeDescriptor.class;
	}
}
